package org.example.aienglishapp.controller.progress;


import lombok.Getter;
import lombok.Setter;
import org.example.aienglishapp.entity.progress.UsersProgress;
import org.example.aienglishapp.service.progress.UserProgressService;
import org.example.aienglishapp.util.exception.ResourceNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user-progress") // 允许来自前端 React 应用的跨域请求
public class UserProgressController {

    private final UserProgressService userProgressService;

    @Autowired
    public UserProgressController(UserProgressService userProgressService) {
        this.userProgressService = userProgressService;
    }

    @GetMapping
    public UsersProgress getUserProgress(@RequestParam Long userId) {
        return userProgressService.getUserProgress(userId)
                .orElseThrow(() -> new ResourceNotFoundException("UserProgress not found for userId: " + userId));
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public UsersProgress createOrUpdateUserProgress(@RequestBody UserProgressRequest userProgressRequest) {
        UsersProgress usersProgress = new UsersProgress();
        usersProgress.setUserId(userProgressRequest.getUserId());
        usersProgress.setVocabularyMastered(userProgressRequest.getVocabularyMastered());
        usersProgress.setListeningHours(userProgressRequest.getListeningHours());
        usersProgress.setMockTestsCompleted(userProgressRequest.getMockTestsCompleted());
        usersProgress.setTotalStudyHours(userProgressRequest.getTotalStudyHours());

        return userProgressService.createOrUpdateUserProgress(usersProgress);
    }

    // 定义内部静态类用于解析用户进度请求
    @Setter
    @Getter
    public static class UserProgressRequest {
        private Long userId;
        private Integer vocabularyMastered;
        private Integer listeningHours;
        private Integer mockTestsCompleted;
        private Integer totalStudyHours;

        // Getters and Setters

    }
}